package com.ctshk.sapp.guide.controller;


import com.alibaba.excel.EasyExcel;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.guide.dto.GuideCollarExcelDTO;
import com.ctshk.rpc.guide.dto.GuideCollarListDTO;
import com.ctshk.rpc.guide.dto.GuideCollarQueryDTO;
import com.ctshk.rpc.guide.req.*;
import com.ctshk.rpc.guide.service.IGuideCollarService;
import com.ctshk.sapp.guide.convert.LocalDateConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 导领信息表 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-25
 */
@Api(value = "导领信息管理",tags = "导领信息管理")
@RestController
@RequestMapping("/guide/collar")
public class GuideCollarController extends BaseController {

    @DubboReference
    private IGuideCollarService guideCollarService;

    @ApiOperation(value = "新增导领信息", notes = "新增导领信息")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.ADD, operTitle = "新增导领信息", operDesc = "新增导领信息")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid GuideCollarAddReq req) {
        return guideCollarService.add(req,getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "查询导领信息列表", notes = "查询导领信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.QUERY, operTitle = "查询领队信息列表", operDesc = "查询领队信息列表")
    @PostMapping("/list")
    public Result<PageResponse<GuideCollarListDTO>> list(@RequestBody @Valid GuideCollarListReq req) {
        return Result.success(guideCollarService.list(req));
    }

    @ApiOperation(value = "查询导领信息", notes = "查询导领信息")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.QUERY, operTitle = "查询领队信息", operDesc = "查询领队信息")
    @PostMapping("/query")
    public Result<GuideCollarQueryDTO> query(@RequestBody @Valid GuideCollarQueryReq req) {
        return Result.success(guideCollarService.query(req));
    }

    @ApiOperation(value = "编辑导领信息", notes = "编辑导领信息")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.UPDATE, operTitle = "编辑领队信息", operDesc = "编辑领队信息")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid GuideCollarUpdateReq req) {
        return guideCollarService.update(req,getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "编辑导领信息(转分社,黑名单,升职,离职)", notes = "编辑导领信息")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.UPDATE, operTitle = "编辑领队信息", operDesc = "编辑领队信息")
    @PostMapping("/updateInfo")
    public Result updateInfoGuideCollar(@RequestBody @Valid GuideCollarUpdateInfoReq req) {
        return guideCollarService.updateInfo(req,getCurrentUser(getRequest()));
    }

    /**
     * 下载导出
     */
    @ApiOperation(value = "导出导领信息表", notes = "导出导领信息表")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.QUERY, operTitle = "导出导领信息表", operDesc = "导出导领信息表")
    @PostMapping("/download")
    public void download(@RequestBody @Valid GuideCollarQueryExcelReq req,HttpServletResponse response) throws IOException {
        List<GuideCollarExcelDTO> listDTO = guideCollarService.selectListByIds(req);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String time = LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String fileName = null;
        if(req.getType() == 1){
            fileName = "领队人员信息表"+time;
        } else if (req.getType() == 2) {
            fileName = "导游人员信息表"+time;
        }
        String sheetName = "人员明细";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), GuideCollarExcelDTO.class).sheet(sheetName).registerConverter(new LocalDateConverter()).doWrite(listDTO);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
